package client;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.spec.X509EncodedKeySpec;

public class leggiChiaviClient {
	
	public leggiChiaviClient(){
		
	}
	
	public static void main(String[] args){
		PublicKey pk=null;
		leggiChiaviClient lc = new leggiChiaviClient();
		pk = lc.getPublic();
		System.out.println(pk);
		
	}
	
	
	public PublicKey getPublic(){
		FileInputStream fis;
		PublicKey publicKey = null;
		
		try {
		
		fis = new FileInputStream("./certificati/clientKeystore.jks");
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		int i=0;
		while((i=fis.read())!=-1){
				baos.write(i);
				}
		fis.close();
		byte[] publicKeyBytes = baos.toByteArray();
		baos.close();
		X509EncodedKeySpec ks = new X509EncodedKeySpec(publicKeyBytes);
		KeyFactory kf = KeyFactory.getInstance("RSA", "BC");
		publicKey = kf.generatePublic(ks);
		} 
		catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally{
			return publicKey;
		}
		
		
	}
	

}
